package com.jkt.rms.dataservice.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

import com.jkt.rms.business.Vendor;
import com.jkt.rms.commons.utils.RMSException;
import com.jkt.rms.constants.IConstants;
import com.jkt.rms.dataservice.AbstractDataService;
import com.jkt.rms.dataservice.VendorDataService;

public class VendorDataServiceImpl extends AbstractDataService<Vendor> implements
VendorDataService {

	@Override
	protected Class<Vendor> getReferenceClass() {
		return Vendor.class;
	}

	public void saveVendor(Vendor vendor) throws RMSException {
		save(vendor);
	}
	
	public void updateVendor(Vendor vendor) throws RMSException {
		saveOrUpdate(vendor);
	}

	public boolean isVendorExist(String vendorCode,String vendorName, Long id) {
		DetachedCriteria firstCriteria = DetachedCriteria.forClass(getReferenceClass());
		DetachedCriteria secondCriteria = DetachedCriteria.forClass(getReferenceClass());
		List<Vendor> vendorList = null;
			if(id != null && (!id.equals(IConstants.EMPTY_STRING))) {
				firstCriteria.add(Expression.ne(Vendor.PROP_ID, id));
				secondCriteria.add(Expression.ne(Vendor.PROP_ID, id));
			}
			if(vendorCode != null && (!vendorCode.equals(IConstants.EMPTY_STRING))) {
				firstCriteria.add(Restrictions.eq(Vendor.PROP_VENDOR_CODE, vendorCode.trim()));
				vendorList = findByCriteria(firstCriteria);
				if(vendorList.isEmpty()) {
					if(vendorName != null && (!vendorName.equals(IConstants.EMPTY_STRING))) {
						secondCriteria.add(Restrictions.eq(Vendor.PROP_VENDOR_NAME, vendorName.trim()));
						vendorList = findByCriteria(secondCriteria);
					}
				} else {
					return true;
				}
		} else if(vendorCode != null && (!vendorCode.equals(IConstants.EMPTY_STRING))) {
			firstCriteria.add(Restrictions.eq(Vendor.PROP_VENDOR_CODE, vendorCode.trim()));
			vendorList = findByCriteria(firstCriteria);
			if(vendorList.isEmpty()) {
				if(vendorName != null && (!vendorName.equals(IConstants.EMPTY_STRING))) {
					secondCriteria.add(Restrictions.eq(Vendor.PROP_VENDOR_NAME, vendorName.trim()));
					vendorList = findByCriteria(secondCriteria);
				}
			} else {
				return true;
			}
		}	
		 
		return !vendorList.isEmpty();
	}

	public List<Vendor> findByCriteria(Vendor vendorData) {
		DetachedCriteria criteria = DetachedCriteria.forClass(getReferenceClass());
		if(vendorData != null) {
			if(vendorData.getVendorCode() != null && (!vendorData.getVendorCode().equals(IConstants.EMPTY_STRING))) {
				//criteria.add(Expression.like(Vendor.PROP_VENDOR_CODE, vendorData.getVendorCode().trim() + MatchMode.ANYWHERE));
				criteria.add(Property.forName("vendorCode").like(vendorData.getVendorCode(), MatchMode.ANYWHERE));
			}
			if(vendorData.getVendorName() != null && (!vendorData.getVendorName().equals(IConstants.EMPTY_STRING))) {
				//criteria.add(Expression.like(Vendor.PROP_VENDOR_NAME, vendorData.getVendorName().trim() + MatchMode.ANYWHERE));
				//criteria.add(Expression.like(Vendor.PROP_STATUS, IConstants.STATUS_YES));
				criteria.add(Property.forName("vendorName").like(vendorData.getVendorName(), MatchMode.ANYWHERE));
				criteria.add(Property.forName("status").eq( IConstants.STATUS_YES));
			}

		}
		return findByCriteria(criteria);
	}
	
	public Vendor findById(Long id) {
		DetachedCriteria criteria = DetachedCriteria.forClass(getReferenceClass());
		if(id != null) {
			criteria.add(Expression.like(Vendor.PROP_ID, id));
		}
		List<Vendor> findByCriteria = findByCriteria(criteria);
		Vendor vendor = findByCriteria.get(0);
		return vendor;
	}
	
	
	@SuppressWarnings("unchecked")
	/**
	 * This method reurns the list of all Vendor
	 */
	public List<Vendor> getActiveVendorList(){
		return getSession().createQuery("from Vendor v where v.status = 'Y'").list();
	}

	
}
